﻿using MES.Core;
using MES.Core.Model;
using SqlSugar;
using System.Reflection;

namespace MES.Service
{
    public class LibraryManagerService
    {

        private readonly SqlSugarHelper sqlSugarHelper;

        public LibraryManagerService()
        {
            sqlSugarHelper = new SqlSugarHelper();
        }


        /// <summary>
        /// 获取库连接池列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<ConnectionPool>> GetLibraryManagerList()
        {
            return await sqlSugarHelper.Db.Queryable<ConnectionPool>().ToListAsync();
        }

        /// <summary>
        /// 添加库连接池
        /// </summary>
        /// <param name="connectionPool"></param>
        /// <returns></returns>
        public async Task<bool> AddLibraryManager(ConnectionPool connectionPool)
        {
            return await sqlSugarHelper.Db.Insertable(connectionPool).ExecuteCommandAsync() > 0;
        }

        /// <summary>
        /// 获取连接
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>

        public async Task<ConnectionPool> GetConnectionPoolAsync(int Id)
        {
            return await sqlSugarHelper.Db.Queryable<ConnectionPool>().Where(x => x.IsEnabled == 1).InSingleAsync(Id);
        }


        /// <summary>
        /// 创建数据库表
        /// </summary>
        /// <param name="IsMainDatabase"></param>
        /// <param name="connectio"></param>
        public void CreateTable(bool IsMainDatabase, ConnectionPool connectio)
        {

            sqlSugarHelper.ChangeDB(connectio.ConnectionString, connectio.DbType);
            sqlSugarHelper.Db.DbMaintenance.CreateDatabase();
            if (IsMainDatabase)
            {
                Type[] typelist = GetTypesWithSugarTableAttributeFromAssembly("MES.Core");
                sqlSugarHelper.Db.CodeFirst.InitTables(typelist);
            }
            else
            {
                Type[] typelist = GetTypesWithSugarTableAttributeFromAssembly("MES.Model");

                sqlSugarHelper.Db.CodeFirst.InitTables(typelist);
            }

        }

        /// <summary>
        /// 获取指定命名空间下的所有类
        /// </summary>
        /// <param name="assembly"></param>
        /// <param name="nameSpace"></param>
        /// <returns></returns>
        private Type[] GetTypesWithSugarTableAttributeFromAssembly(string assemblyName, string targetNamespace = null)
        {
            Assembly assembly = Assembly.Load(assemblyName);
            Type[] allTypes = assembly.GetTypes();

            List<Type> resultTypes = new List<Type>();

            foreach (Type type in allTypes)
            {
                if (targetNamespace == null || type.Namespace == targetNamespace)
                {
                    object[] attributes = type.GetCustomAttributes(typeof(SugarTable), false);
                    if (attributes.Length > 0)
                    {
                        resultTypes.Add(type);
                    }
                }
            }

            return resultTypes.ToArray();
        }
    }
}
